package com.zhuang.controller;

import com.zhuang.dto.QuestionDTO;
import com.zhuang.mapper.QuestionMapper;
import com.zhuang.pojo.Question;
import com.zhuang.pojo.User;
import com.zhuang.service.NotificationService;
import com.zhuang.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/publish")
public class PublishController {


    @Autowired
    private QuestionService questionService;

    @Autowired
    NotificationService notificationService;

    @GetMapping
    public String publish(Model model,
                          HttpSession session) {
        // 计算消息未读数
        User user = (User) session.getAttribute("user");
        Integer count = notificationService.count(user.getAccountId().longValue());
        model.addAttribute("count", count);
        model.addAttribute("title", null);
        model.addAttribute("description", null);
        model.addAttribute("tag", null);
        model.addAttribute("id", null);
        return "publish";
    }

    @GetMapping("/{id}")
    public String edit(@PathVariable(value = "id")Integer id,
                          Model model,
                       HttpSession session) {
        // 计算消息未读数
        User user = (User) session.getAttribute("user");
        Integer count = notificationService.count(user.getAccountId().longValue());
        model.addAttribute("count", count);

        QuestionDTO question = questionService.getQuestion(id);
        model.addAttribute("title", question.getTitle());
        model.addAttribute("description", question.getDescription());
        model.addAttribute("tag", question.getTag());
        model.addAttribute("id", question.getId());
        return "publish";
    }

    @PostMapping()
    public String create(@RequestParam("title") String title,
                         @RequestParam("description") String description,
                         @RequestParam("tag") String tag,
                         @RequestParam(value = "id", required = false) Integer id,
                         HttpSession session,
                         Model model) {
        model.addAttribute("title", title);
        model.addAttribute("description", description);
        model.addAttribute("tag", tag);
        // 计算消息未读数
        User user = (User) session.getAttribute("user");
        Integer count = notificationService.count(user.getAccountId().longValue());
        model.addAttribute("count", count);

        if (title == null || "".equals(title)) {
            model.addAttribute("msg", "标题不能为空");
            return "publish";
        }
        if (description == null || "".equals(description)) {
            model.addAttribute("msg", "描述不能为空");
            return "publish";
        }
        if (tag == null || "".equals(tag)) {
            model.addAttribute("msg", "标签不能为空");
            return "publish";
        }

        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setGmtCreate(System.currentTimeMillis());
        question.setGmtModified(question.getGmtCreate());
        question.setViewCount(0);
        question.setCommentCount(0);
        question.setLikeCount(0);
        question.setCreator(user.getAccountId());
        if (id != null) {
            question.setId(id);
        }
        questionService.crateOrUpdate(question);
        return "redirect:/";
    }
  }
